package com.nexus.plugin.security;

import com.nexus.core.auth.LoginUser;
import com.nexus.core.utils.ObjectUtils;
import com.nexus.plugin.redis.RedisService;
import com.nexus.plugin.security.constants.AuthConstants;

import java.util.concurrent.TimeUnit;

/**
 * 登录用户缓存
 *
 * @Date 2025/8/1 15:28
 * @Author luzhengning
 **/
public class SecurityCache {

    /**
     * 保存用户到redis
     *
     * @author luzhengning
     * @date 2023/3/20 13:54
     */
    public static void setLoginUser(String username, LoginUser loginUser) {
        RedisService.getInstance().setEx(AuthConstants.Redis_Key_SysLoginUserList + username, loginUser, AuthConstants.ACCESS_TOKEN_EXPIRE_HOUR, TimeUnit.HOURS);
    }

    /**
     * 获取登录用户
     * 从redis中读取用户
     *
     * @author luzhengning
     * @date 2023/3/20 13:54
     */
    public static LoginUser getLoginUser(String username) {
        Object value = RedisService.getInstance().get(AuthConstants.Redis_Key_SysLoginUserList + username);
        if (ObjectUtils.isNotEmpty(value)) {
            return (LoginUser) value;
        }
        return null;
    }

}
